package com.hhsj.design.pullableview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hhsj.design.pullableview.R;
import com.hhsj.design.pullableview.ability.IPullUpAble;


public class DefaultLoadMoreView extends ILoadMoreView {
    private ProgressBar progressBar;
    private TextView statusTextView;

    public DefaultLoadMoreView(Context context) {
        this(context,R.layout.layout_default_refreshview);
    }

    public DefaultLoadMoreView(Context context, int layoutId) {
        super(context, layoutId);
    }

    public DefaultLoadMoreView(Context context, View view) {
        super(context, view);
    }

    @Override
    public void createView(View view) {
        view.setBackgroundColor(0x238F2383);
        view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getLoadMoreHeight()));
        progressBar = view.findViewById(R.id.progress_bar);
        progressBar.setIndeterminateDrawable(mContext.getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        progressBar.setProgressDrawable(mContext.getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        statusTextView = view.findViewById(R.id.refresh_text);
        statusTextView.setText("下拉刷新");
    }

    @Override
    public ViewGroup.MarginLayoutParams createLayoutParams() {
        return new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getLoadMoreHeight());
    }

    @Override
    public int getLoadMoreHeight() {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, mContext.getResources().getDisplayMetrics());
    }

    @Override
    public void startAnim() {
        progressBar.setIndeterminateDrawable(mContext.getResources().getDrawable(R.drawable.progressbar_cycle));
        progressBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.progressbar_cycle));
        progressBar.invalidate();
    }

    @Override
    public void stopAnim() {
        progressBar.setIndeterminateDrawable(mContext.getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        progressBar.setProgressDrawable(mContext.getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        progressBar.invalidate();
    }

    @Override
    public void updateText(String... text) {
        statusTextView.setText(text[0]);
    }

    @Override
    public void updateWithState(IPullUpAble.FooterState state) {
        switch (state) {
            case TO_NORMAL:
                statusTextView.setText("上拉加载更多");
                break;
            case NORMAL:
                statusTextView.setText("上拉加载更多");
                break;
            case LOADING:
                statusTextView.setText("正在加载");
                break;
            case TO_LOAD:
                statusTextView.setText("松开加载更多");
                break;
            case CAN_LOAD:
                statusTextView.setText("松开加载更多");
                break;
            case FOR_LOAD:
                statusTextView.setText("上拉加载更多");
                break;
        }
    }

    @Override
    public int getBottomMargin() {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mView.getLayoutParams();
        return lp.bottomMargin;
    }
}
